﻿Public Class clsCAMARA
    Dim CapHwnd As Integer
    'Tamaño de la ventana del webcam
    Dim Ancho As Integer = 333
    Dim Alto As Integer = 357 ' 240

    '"libreria.DLL" = El nombre del API que se desea importar
    ' EntryPoint = indica el nombre exacto de la función del API que queremos usar
#Region "Librerias DLL"
    'La función SendMessage llama al procedimiento de ventana para la ventana especificada y no vuelve hasta que el procedimiento de ventana se ha procesado el mensaje
    'LRESULT SendMessage(
    'HWND hWnd, // handle of destination window
    'UINT Msg, // message to send
    'WPARAM wParam, // first message parameter
    'LPARAM lParam // second message parameter
    ');

    'Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    'End Function
    'Crea una ventana de captura
    'Public Shared Function capCreateCaptureWindowA(ByVal Nombre As String, ByVal dwStyle As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hWnd As Integer, ByVal nID As Integer) As Integer
    'End Function
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Short, ByVal lParam As Integer) As Integer
    Private Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Short, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer

#End Region

#Region "Constantes API – no se que significan todas  "
    Const WM_USER As Integer = 1024
    Const WM_CAP_CONNECT As Integer = 1034
    Const WM_CAP_DISCONNECT As Integer = 1035
    Const WM_CAP_GET_FRAME As Integer = 1084
    Const WM_CAP_COPY As Integer = 1054
    Const WM_CAP_START As Integer = WM_USER
    Const WM_CAP_SET_PREVIEWRATE As Integer = WM_USER + 52
    Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP_START + 41
    Const WM_CAP_DLG_VIDEOSOURCE As Integer = WM_CAP_START + 42
    Const WM_CAP_DLG_VIDEODISPLAY As Integer = WM_CAP_START + 43
    Const WM_CAP_GET_VIDEOFORMAT As Integer = WM_CAP_START + 44
    Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_CAP_START + 45
    Const WM_CAP_DLG_VIDEOCOMPRESSION As Integer = WM_CAP_START + 46
    Const WM_CAP_SET_PREVIEW As Integer = WM_CAP_START + 50
#End Region

    'Captura frame y deposita en el portapapeles
    Public Sub timer_tick(ByVal picture As PictureBox)
        SendMessage(CapHwnd, WM_CAP_GET_FRAME, 0, 0)
        SendMessage(CapHwnd, WM_CAP_COPY, 0, 0)
        picture.Image = Clipboard.GetImage()
        Application.DoEvents()
    End Sub
    'guarda el frame que esta en memoria en un archivo JPG
    Public Sub Capturar(ByVal picture As PictureBox)
        Dim sfile_JPG As String = Application.StartupPath & "\ARTICULO.jpg"
        Dim obj_bitMap As New Bitmap(Ancho, Alto)

        Try
            picture.DrawToBitmap(obj_bitMap, New Rectangle(0, 0, Ancho, Alto))
            obj_bitMap.Save(sfile_JPG, Imaging.ImageFormat.Jpeg)

            obj_bitMap.Dispose()

        Catch ex As Exception
            System.Console.WriteLine(ex)
        End Try
    End Sub

    Public Function Iniciar(ByVal timer As Timer, ByVal padre As Form) As Integer
        Try
            'Configura la ventana de captura
            CapHwnd = capCreateCaptureWindowA("WebCam", 0, 0, 0, Ancho, Alto, padre.Handle.ToInt32(), 0)
            Application.DoEvents()
            SendMessage(CapHwnd, WM_CAP_CONNECT, 0, 0)
            SendMessage(CapHwnd, WM_CAP_SET_PREVIEWRATE, 69, 0)
            SendMessage(CapHwnd, WM_CAP_SET_PREVIEW, 0, 0)
            'se inicia el Timer
            timer.Start()
            Iniciar = 0
        Catch ex As Exception
            Iniciar = 1


        End Try
    End Function
    Public Sub Detener(ByVal timer As Timer)
        Try
            timer.Stop()
            Application.DoEvents()
            SendMessage(CapHwnd, WM_CAP_DISCONNECT, 0, 0)
        Catch ex As Exception
            Console.WriteLine(ex)
        End Try
    End Sub
End Class
